package cn.labbook.bluetooth.util;

import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2018/11/12.
 */
@Log4j2
public class TimeUtils {

    /**
     * 获取今凌晨0点0分0秒的date对象
     * num 代表加减的天数
     *
     * @return
     */
    public static Date get000DateD(int num) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, num);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        Date current = c.getTime();
        return current;
    }

    /**
     * 获取今凌晨0点0分0秒的date对象
     * num 代表加减的月数
     *
     * @return
     */
    public static Date get000DateM(int num) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, num);
        c.set(Calendar.DAY_OF_MONTH,1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        Date current = c.getTime();
        return current;
    }

    /**
     *
     *
     * @return
     */
    public static Date get000DateY(int num) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, num);
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        Date current = c.getTime();
        return current;
    }

    /**
     * 当前时间加n天
     * @return
     * @throws ParseException
     */
    public static Date addDate(int n) throws ParseException {

        Calendar c = Calendar.getInstance();
        c.setTime(getNewDay());
        c.add(Calendar.DATE, n);
        return c.getTime();
    }

    /**
     * 时间加n月
     * @return
     * @throws ParseException
     */
    public static Date addMonth(int n,Date date) throws ParseException {

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, n);
        return c.getTime();
    }
    /**
     * 获取当前日期不包含时分秒
     * @return
     * @throws ParseException
     */
    public static Date getNewDay() throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(new Date());
        Date date = sdf.parse(s);
        return date;
    }

    /**
     * 时间格式化
     * @param date
     * @param format
     * @return
     */
    public static String dateFormat(Date date, String format) {
        String result = null;
        if (date != null) {
            if (StringUtils.isEmpty(format)) {
                format = "yyyy-MM-dd";
            }

            try {
                result = DateFormatUtils.format(date, format);
            } catch (Exception var4) {
                return null;
            }

        }
        return result;
    }

    public static String getDateTime() {
        return dateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 时间做差值
     * @param fDate
     * @param sDate
     * @return
     */
    public static int getBetweenSeconds(Date fDate, Date sDate) {
        int day = (int)((fDate.getTime() - sDate.getTime()) / 1000L);
        return day;
    }

    /**
     * 字符串转date
     * @param str
     * @param format
     * @return
     */
    public static Date stringToDate(String str, String format) {
        if (StringUtils.isEmpty(str)) {
            return null;
        } else {
            Date result = null;
            String[] formats ;
            if (StringUtils.isEmpty(format)) {
                formats = new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss", "yyyyMMdd HH:mm:ss"};
            } else {
                formats = new String[]{format, "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss", "yyyyMMdd HH:mm:ss"};
            }

            try {
                result = DateUtils.parseDate(str, formats);
            } catch (Exception var5) {
                log.warn("日期或时间格式不正确，日期时间字符串：" + str + "， 格式：" + format);
            }

            return result;
        }
    }

    /**
     * 获得当前年
     */
    public static int getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int year = calendar.get(Calendar.YEAR);
        return year;
    }

    /**
     * 获得程序运行当前月
     */
    public static int getCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int month = calendar.get(Calendar.MONTH) + 1;
        return month;
    }

    /**
     * 获得某个日期所属年份
     */
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        return year;
    }

    /**
     * 获得某个时间所属月
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        return month;
    }

    public static String format(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        return DateFormatUtils.format(date, pattern);
    }


}
